﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLayer;
using System.Transactions;

namespace Stationery_Store_Inventory_Management
{
    public class DisbursementListController
    {
        DisbursementListDAL dDal;
        StationeryRepository stationeryRepo;

        public DisbursementListController()
        {
            dDal = new DisbursementListDAL();
            stationeryRepo = new StationeryRepository();
        }

        public List<CollectionPoint> getCollectionPointNamesCont()
        {
            List<CollectionPoint> colPointNames = dDal.getCollectionPointNames();
            return colPointNames;
        }

        public List<Category> getCategoriesCont()
        {
            List<Category> cat = dDal.getCategories();
            return cat;
        }

        public List<Stationery> getStationeryNamesCont(int categId)
        {
            List<Stationery> statNames = dDal.getStationeryNames(categId);
            return statNames;
        }

        public IEnumerable<Object> getDeptDetailsCont(int cId)
        {
            var result = dDal.getDeptDetails(cId);
            return result;
        }

        public IEnumerable<Object> getDisbursementDetailsListCont(String dCode)
        {
            var disbursementDetailsList = dDal.getDisburementDetailsList(dCode);
            return disbursementDetailsList;
        }

        public void updDisbursementDetailsCont(String dCode,String empId)
        {
            dDal.updDisbursementDetails(dCode, empId);
        }

        public IEnumerable<Object> getDisbursementDeptQtybyItemCont(int cPointId, string ItemName)
        {
            var dDeptQtybyItems = dDal.getDisbursementDeptQtybyItem(cPointId, ItemName);
            return dDeptQtybyItems;
        }

        public void updAdjustmentandStockHistoryCont(int qty, string iname)
        {
            dDal.updAdjustmentandStockHistory(qty, iname);
        }

        public void updActualQtyCont(int DepRequestId,int actQty)
        {
            dDal.updateActualQty(DepRequestId,actQty);
        }

        public DateTime getColPointTimeCont(string cpName)
        {
            DateTime cpTime = dDal.getCollectionPointTime(cpName);
            return cpTime;
        }

        public string getRepNameCont(string dName)
        {
            string rName = dDal.getDeptRepName(dName);
            return rName;
        }

        public DateTime getCollectionDateCont(string dName)
        {
            DateTime colDate = dDal.getCollectionDate(dName);
             return colDate;
        }

        public void updateDeptReqDetActualQty(List<int> lstDeptReqDetId, List<int> lstAllocatedQty)
        {
            dDal.updateDeptReqDetActualQty(lstDeptReqDetId, lstAllocatedQty);

        }

        public Stationery GetStationeryByItemCode(string itemCode)
        {
            return stationeryRepo.GetStationeryByItemCode(itemCode);
        }

        public void UpdateDeptReqDetAndAdjAndStockHistory(int adjustAmount, string itemName, List<int> lstDeptReqDetId, List<int> lstAllocatedQty)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                // Adjustment
                updAdjustmentandStockHistoryCont(adjustAmount, itemName);

                // Allocate
                updateDeptReqDetActualQty(lstDeptReqDetId, lstAllocatedQty);
                ts.Complete();
            }
        }

    }
}